package cn.edu.scau.cmi.linzhanyi.composite;

import java.util.ArrayList;
import java.util.List;

public class Group implements Component {
    private String name;
    private Component parent;
    private List<Component> list;

    public Group(String name) {
        this.name = name;
    }

    @Override
    public void addAll(Component... c) {
        if (list==null){
            list=new ArrayList<>();
            for(Component c1 :c){
                c1.setParent(this);
                list.add(c1);

            }


        }
        else {
            for(Component c1 :c){
                c1.setParent(this);
                list.add(c1);

            }

        }
    }

    @Override
    public String say(String s) {
        s+="        ";
        String say=s+"组别"+name+":\n"+s+"{\n";
        if (list!=null)
        for(Component c : list){
            say+=c.say(s)+'\n';
        }
        say+=s+"}\n";
        return say;
    }

    @Override
    public Component getParent() {
        return parent;
    }

    @Override
    public void setParent(Component parent) {
        this.parent=parent;
    }

    @Override
    public void add(Component c) {
        if (list==null){
            list=new ArrayList<>();
            c.setParent(this);
            list.add(c);

        }
        else {
            c.setParent(this);
            list.add(c);

        }
    }

    @Override
    public void remove(Component c) {
        list.remove(c);
    }

    @Override
    public List<Component> get() {
        return list;
    }

    @Override
    public String getNmae() {
        return name;
    }

    @Override
    public void setNmae(String name) {
        this.name=name;
    }
    @Override
    public String toString(){
        return name;
    }

}
